package com.leyantech.ecp.agenx.loadtest.converter;

import com.leyantech.ecp.agenx.common.utils.JsonUtils;
import com.leyantech.ecp.utils.collection.RandomUtils;
import com.leyantech.utility.config.ConfigUtil;

import com.google.gson.reflect.TypeToken;
import com.google.inject.Singleton;
import io.vertx.core.json.JsonObject;

import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Supplier;

/**
 * @author eahau, {@literal <eahau@foxmail.com>}
 * @date 2022-11-18.
 */
@Singleton
public class LoadtestMessageConverter {

  static final Supplier<Set<Entry<String, Set<String>>>> loadtestConfig = ConfigUtil
      .getDynamicConfig(
          "loadtest",
          "store.set",
          json -> JsonUtils.fromJson(json, new TypeToken<Map<String, Set<String>>>() {
          }).entrySet(),
          Collections.emptySet()
      );

  public JsonObject apply(JsonObject jsonObject) {

    final Entry<String, Set<String>> storeIdAndAssistantIdSet = RandomUtils
        .randomValue(loadtestConfig.get());

    final String sid = storeIdAndAssistantIdSet.getKey();
    final String aid = RandomUtils.randomValue(storeIdAndAssistantIdSet.getValue());

    jsonObject.put("sid", sid).put("aid", aid);

    return jsonObject;
  }

}
